/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilities;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Tobias
 */
public class FileScanner {
    
    private File f;
    private Scanner sc;
    private String[] lines;
    private boolean readmode;
    private FileWriter fw=null;
    
    public FileScanner(File f,boolean readmode){
           
        this.f=f;
        this.readmode=readmode;
        if(readmode){
            try {
                sc = new Scanner(f);
            } catch (FileNotFoundException ex) {
                 JOptionPane.showMessageDialog(null, ex.getMessage()); //TODO: remove
                Logger.getLogger(FileScanner.class.getName()).log(Level.SEVERE, null, ex);
                //TODO: do something I understand instead
                return;
            }
            readContent();
        }else{
            if(!f.exists()){
                try {
                    f.createNewFile();
                
                } catch (IOException ex) {
                      JOptionPane.showMessageDialog(null, "Could not create Outputfile"); //TODO: remove
                    Logger.getLogger(FileScanner.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            try {
                fw = new FileWriter(f);
            } catch (IOException ex) {
                 JOptionPane.showMessageDialog(null, "Could not open Outputfile"); //TODO: remove
                Logger.getLogger(FileScanner.class.getName()).log(Level.SEVERE, null, ex);
                return;
            }
        }
    }
    
    public FileScanner(File f){
        this(f,true);
    }
    
    private void readContent(){
       ArrayList<String> temp = new ArrayList<String>();
        while(sc.hasNextLine()){
           temp.add(sc.nextLine());
        }
        this.lines = temp.toArray(new String[temp.size()]);
        
    }
    
    public String[] getContent(){
        return lines;
    }
    
    public void writeContent(String[] lines){
        if(readmode) throw new RuntimeException("Cannot write to File in read-only mode");
        for(int i=0;i<lines.length;i++){
            writeContent(lines[i]+"\n");
        }
    }
    public void writeContent(String line){
         if(readmode) throw new RuntimeException("Cannot write to File in read-only mode");
         if(fw==null)throw new RuntimeException("File isnt open!");
        try {
            fw.write(line);
        } catch (IOException ex) {
            System.out.println("Could not write a Line to file");
            Logger.getLogger(FileScanner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void finish(){
        if(sc!=null)sc.close();
        if(fw!=null){
            try {
                fw.close();
            } catch (IOException ex) {
                Logger.getLogger(FileScanner.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
}
